<?php

/**
 * This is the model class for table "requests".
 */
class Requests extends CActiveRecord {
    /**
     * The followings are the available columns in table 'requests':
     * @var integer $id
     * @var integer $courseWork_id
     * @var integer $student_id
     * @var string $coverletter
     * @var integer $archived
     */

    /**
     * Returns the static model of the specified AR class.
     * @return Requests the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'requests';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array('courseWork_id, student_id, coverletter, archived', 'required'),
                array('courseWork_id, student_id, archived', 'numerical', 'integerOnly'=>true),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id, courseWork_id, student_id, coverletter, archived', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
                'courseWork' => array(self::BELONGS_TO, 'Courseworks', 'courseWork_id'),
                'student' => array(self::BELONGS_TO, 'Student', 'student_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'id' => 'ID',
                'courseWork_id' => 'Course Work',
                'student_id' => 'Student',
                'coverletter' => 'Сопроводительное письмо',
                'archived' => 'Archived',
                'courseWork.name'=>'Название темы',
                'courseWork.year'=>'Курс',
                'courseWork.professor.email'=>'Эл. адрес преподавателя',

        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);

        $criteria->compare('courseWork_id',$this->courseWork_id);

        $criteria->compare('student_id',$this->student_id);

        $criteria->compare('coverletter',$this->coverletter,true);

        $criteria->compare('archived',$this->archived);

        return new CActiveDataProvider(get_class($this), array(
                        'criteria'=>$criteria,
        ));
    }
    public function getStudentInfo() {
        return $smodel=Student::model()->findByPk(Yii::app()->user->id);

    }
    public function getAvailiable($varid) {

        $student=Student::model()->findByPk($varid);
        //выбор id факультета студента
        $faculty= Faculty::model()->findByPk($student->faculty_id);


        //выбор всех кафедр, связанных с факультетом
        $com1=Yii::app()->db->createCommand("SELECT id from department where faculty_id=".$faculty->id);
        $dep=$com1->queryAll();

        //выбор всех профессоров, связанных со всеми факультетами, к которым относится студент
        $pr=new CList;
        foreach ($dep as $val) {
            foreach($val as $b) {
                $com2=Yii::app()->db->createCommand("SELECT id from professor where department_id=".$b);
                $pr[]=$com2->queryAll($com2);
            }
        }

        //выбираем все темы подходящие для нашего студента


        $cr=new CList;
        $courseworks=new CourseWorks;
        $criteria=new CDbCriteria();
        $i=0;
        $cw = new CList;

        foreach($pr as $val) {
            foreach($val as $str) {
                foreach($str as $d) {
                 
                   $criteria->condition='professor_id=:idd AND year=:yea';
                   $criteria->params=array('idd'=>$d,':yea'=>$student->year);
                    $count=CourseWorks::model()->count($criteria);
                    $courseworks=CourseWorks::model()->findAll($criteria);                    
                    if($count==1) {
                        $cw[]=$courseworks;
                    }
                    else {
                        foreach($courseworks as $onecoursework) {
                            $cw[]=$onecoursework;
                        }
                    }
                }
            }
        }
        return $cw;

    }

}